<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace App\Model\LazyCommon;

use App\Base\BaseModel;
use App\Entity\ConfCat;
use Doctrine\Bundle\DoctrineBundle\Registry;
use App\Setting\LazyCommon\ConfCatSetting;
use Symfony\Component\HttpFoundation\Request;

class ConfCatModel extends BaseModel{


    /*
     * 列表基本设置【通用模块必须】
     */
    public function listSettingDefault()
    {
        $settings = new ConfCatSetting($this->getDoctrine());
        return $settings->listConfig();
    }

    //获取所有分类
    public function getAllConfCat(){
        $qb = $this->getQueryBuilder();
        $qb->select("*")
        ->from("Conf_Cat");
        return $qb->execute()->fetchAll();
    }

    //获取所有分类并构建分类树
    public function getAllConfCatTree()
    {
        $categories = $this->getAllConfCat();
        $ConfCatTree = $this->buildeCatTreeArray($categories);
        $data = array();
        foreach ($ConfCatTree as $class) {
            $prefix = "";
            if ($class["level"] > 0) {
                $prefix .="├";
                for ($i=0;$i<$class["level"];$i++ ) {
                    $prefix .="─";
                }
            }
            $data[] = array(
                "id"=>$class["id"],
                "name"=>$prefix.$class["name"],
            );
        }
        return $data;
    }

    //获取第一层分类
    public function getRootConfCat()
    {
        $qb = $this->getQueryBuilder();
        $qb->select("*")
            ->where("level = ?")
            ->setParameter(0,"1")
            ->from("Conf_Cat");
        return $qb->execute()->fetchAll();
    }
    /*
     * 通用Rep
     */
    public function getRep()
    {
        $settings = new ConfCatSetting($this->getDoctrine());
        return $this->em->getRepository($settings->bundleName.":".$settings->entityName);
    }


    public function getSons($parent)
    {
        $qb = $this->getQueryBuilder();
        $qb->select("*")
            ->where("parent = ?")
            ->setParameter(0,$parent)
            ->orderBy("orders")
            ->from("Conf_Cat");
        return $qb->execute()->fetchAll();
    }

    public function getSonsByCode($code)
    {
        $qb = $this->getQueryBuilder();
        $qb->select("*")
            ->where("link = ?")
            ->setParameter(0,$code)
            ->orderBy("orders")
            ->from("Conf_Cat");
        $parent = $qb->execute()->fetch();
        $sons = $this->getSons($parent["id"]);
        return $sons;
    }

    public function getSons4Setting($code)
    {
        $data = $this->getSonsByCode($code);
        $return = array();
        $return[0] = "请选择";
        foreach ($data as $row) {
            $return[$row["id"]] = $row["name"];
        }
        return $return ;
    }
    public function getSons4SettingRadio($code)
    {
        $data = $this->getSonsByCode($code);
        $return = array();
        foreach ($data as $row) {
            $return[$row["id"]] = $row["name"];
        }
        return array_flip($return) ;
    }
    public function getSons4List($code)
    {
        $data = $this->getSonsByCode($code);
        $return = array();
        foreach ($data as $row) {
            $return[] = array(
                "id"    => $row["id"],
                "name"  => $row["name"]
            );
        }
        return $return ;
    }

    /**
     * @param $link
     * @return null|ConfCat
     */
    public function getOneByLink($link)
    {
        return $this->getRep()->findOneBy(array("link"=>$link));
    }

    public function getBool4Setting()
    {
        return ["other"=>["choices"=>["是"=>"1","否"=>"0"]]];
    }
}